
*************************************************;
* Step I generate MA penetration for each year **;
*************************************************;

options ls=76 nofmterr;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';
libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';

data plan0; set new.excl_plans;

   keep org_id org_name totn_03 totn_04 totn_05 totn_06 totn_07 totn_08;

data plan; set plan0;

   if totn_03>0 then in2003=1; else in2003=0;
   if totn_04>0 then in2004=1; else in2004=0;
   if totn_05>0 then in2005=1; else in2005=0;
   if totn_06>0 then in2006=1; else in2006=0;
   if totn_07>0 then in2007=1; else in2007=0;
   if totn_08>0 then in2008=1; else in2008=0;

   up2004=sum(in2003,in2004);
   up2005=sum(in2003,in2004,in2005);
   up2006=sum(in2003,in2004,in2005,in2006);
   up2007=sum(in2003,in2004,in2005,in2006,in2007);

   if up2004=0 and in2005=1 then new2005=1; else if in2005=1 then new2005=0;
   if up2005=0 and in2006=1 then new2006=1; else if in2006=1 then new2006=0;
   if up2006=0 and in2007=1 then new2007=1; else if in2007=1 then new2007=0;
   if up2007=0 and in2008=1 then new2008=1; else if in2008=1 then new2008=0;

   proc sort; by org_id;

data a04; set new.hedis2004;

    ***************************************************;
    **  Demographic Variables and Sample Selection   **;
    ***************************************************;

     if denom_flag=1;
     if state=' ' or state='00' or state > "53" or state in ("40","48") then delete;

     in2004=1;

     length stcounty $ 5;
     stcounty=state||county;

     length plancounty $ 10;
     plancounty=org_id||stcounty;
 
     proc means data=a04 sum noprint nway;
          class plancounty;
          var in2004;
          output out=b04 sum=ma2004;
   
     proc means data=a04(where=(plan_type='HMO - Health Maintenance Organization')) sum noprint nway;
          class plancounty;
          var in2004;
          output out=c04 sum=hmo2004;

 data d04; set a04;

      keep org_id org_name in2004 state county stcounty plancounty plan_type;
      proc sort nodupkey; by plancounty; 

 data ma2004; merge b04 c04 d04; by plancounty;


data a05; set new.hedis2005;

     in2005=1;

     length stcounty $ 5;
     stcounty=state_code||bene_county_cd;

     length plancounty $ 10;
     plancounty=org_id||stcounty;

     proc means data=a05 sum noprint nway;
          class plancounty;
          var in2005;
          output out=b05 sum=ma2005;

     proc means data=a05(where=(plan_type='HMO/HMOPOS')) sum noprint nway;
          class plancounty;
          var in2005;
          output out=c05 sum=hmo2005;

 data d05; set a05;

      keep org_id org_name in2005 state_code bene_county_cd stcounty plancounty plan_type;
      proc sort nodupkey; by plancounty;

 data ma2005; merge b05 c05 d05; by plancounty;


data a06; set new.hedis2006;

     in2006=1;

     length stcounty $ 5;
     stcounty=state_code||bene_county_cd;

     length plancounty $ 10;
     plancounty=org_id||stcounty;

     proc means data=a06 sum noprint nway;
          class plancounty;
          var in2006;
          output out=b06 sum=ma2006;

     proc means data=a06(where=(plan_type='HMO/HMOPOS')) sum noprint nway;
          class plancounty;
          var in2006;
          output out=c06 sum=hmo2006;

 data d06; set a06;

      keep org_id org_name in2006 state_code bene_county_cd  stcounty plancounty plan_type;
      proc sort nodupkey; by plancounty;

 data ma2006; merge b06 c06 d06; by plancounty;



data a07; set new.hedis2007;


     in2007=1;

     length stcounty $ 5;
     stcounty=state_code||bene_county_cd;

     length plancounty $ 10;
     plancounty=org_id||stcounty;

     proc means data=a07 sum noprint nway;
          class plancounty;
          var in2007;
          output out=b07 sum=ma2007;

     proc means data=a07(where=(plan_type='HMO/HMOPOS')) sum noprint nway;
          class plancounty;
          var in2007;
          output out=c07 sum=hmo2007;

 data d07; set a07;

      keep org_id org_name in2007 state_code bene_county_cd  stcounty plancounty plan_type;
      proc sort nodupkey; by plancounty;

 
 data ma2007; merge b07 c07 d07; by plancounty;


 data allma; merge ma2004 ma2005 ma2006 ma2007; by plancounty;


      proc means data=allma sum noprint nway;
           class stcounty;
           var ma2004 ma2005 ma2006 ma2007 hmo2004 hmo2005 hmo2006 hmo2007;  
           output out=allma0 sum=ctyma2004 ctyma2005 ctyma2006 ctyma2007 ctyhmo2004 ctyhmo2005 ctyhmo2006 ctyhmo2007;

      proc sort data=allma; by stcounty;
 
 data allma01; set allma0;

      proc sort; by stcounty;

data ctypop0; set new.ctypop;

     proc sort; by stcounty;


data pene0; merge allma01(in=a) ctypop0; by stcounty;

     if a;

     if ctypop2004<10 then do; ctypop2004=.; ctyma2004=.; ctyhmo2004=.; end;
     if ctypop2005<10 then do; ctypop2005=.; ctyma2005=.; ctyhmo2005=.; end;
     if ctypop2006<10 then do; ctypop2006=.; ctyma2006=.; ctyhmo2006=.; end;
     if ctypop2007<10 then do; ctypop2007=.; ctyma2007=.; ctyhmo2007=.; end;

     if ctypop2004=. and ctypop2005=. and ctypop2006=. and ctypop2007=. then delete;

     cty_mapene2004=ctyma2004/ctypop2004;
     cty_mapene2005=ctyma2005/ctypop2005;
     cty_mapene2006=ctyma2006/ctypop2006;
     cty_mapene2007=ctyma2007/ctypop2007;

     cty_hmopene2004=ctyhmo2004/ctypop2004;
     cty_hmopene2005=ctyhmo2005/ctypop2005;
     cty_hmopene2006=ctyhmo2006/ctypop2006;
     cty_hmopene2007=ctyhmo2007/ctypop2007;

     proc sort; by stcounty;


data allpene; merge allma pene0; by stcounty;

     proc sort; by org_id;


data one; merge allpene plan; by org_id;


     proc means mean median;
          class new2005; var cty_hmopene2004;
          weight totn_05;
     proc means mean median;
          class new2006; var cty_hmopene2005;
          weight totn_06;

     proc means mean median;
          class new2007; var cty_hmopene2006;
          weight totn_07;

     proc means mean median;
          class new2008; var cty_hmopene2007;
          weight totn_08;


endsas;